﻿using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
using LightSwitchApplication.UserCode;
namespace LightSwitchApplication
{
    public partial class RecieptListByRefListDetail
    {
        private bool IsLoaded;
        private DialogHelper dialogHelper;
        private DialogHelper dialogGetSO;
        partial void RecieptListByRefListDetail_Created()
        {
            // Write your code here.
            dialogHelper.InitializeUI();
            dialogGetSO.InitializeUI();
            this.FindControl("RecieptListDetails1").ControlAvailable += new EventHandler<ControlAvailableEventArgs>(RecieptListByRefListDetail_ControlAvailable);
        }

        void RecieptListByRefListDetail_ControlAvailable(object sender, ControlAvailableEventArgs e)
        {
            var datagrid = e.Control as System.Windows.Controls.DataGrid;
            if (datagrid != null)
            {
                foreach (var column in datagrid.Columns)
                {
                    if (column.SortMemberPath == "RefID")
                    {
                        column.IsReadOnly = true;
                    }
                }
            }  
        }

        partial void RecieptListByRefListDetail_InitializeDataWorkspace(List<IDataService> saveChangesTo)
        {
            // Write your code here.
            dialogHelper = new DialogHelper(this.RecieptListByRef, "Group3", 500, -1);
            dialogGetSO = new DialogHelper(this.RecieptListDetails, "Group4", 400, 200);
        }

        partial void RecieptListListAddAndEditNew_CanExecute(ref bool result)
        {
            // Write your code here.
            result = dialogHelper.CanAdd(); 
        }

        partial void RecieptListListAddAndEditNew_Execute()
        {
            // Write your code here.
            dialogHelper.AddEntity();  
        }

        partial void btnNewOK_Execute()
        {
            // Write your code here.
            dialogHelper.DialogOK(); 
        }

        partial void btnNewCancel_Execute()
        {
            // Write your code here.
            dialogHelper.DialogCancel(); 
        }

        partial void GetSO_Execute()
        {
            // Write your code here.
            dialogGetSO.EditSelectedEntity("Chọn hóa đơn tham chiếu");
        }

        partial void btnOK_Execute()
        {
            // Write your code here.
            dialogGetSO.DialogOK();
        }

        partial void btnCancel_Execute()
        {
            // Write your code here.
            dialogGetSO.DialogCancel();
        }

        partial void RecieptListByRef_Loaded(bool succeeded)
        {
            if (!succeeded) return;
            if (string.IsNullOrEmpty(this.RecieptListRefID)) return;
            IsLoaded = true;
        }

        partial void RecieptListByRefListDetail_Activated()
        {
            // Write your code here.
            if (IsLoaded)
            {
                decimal amtReciept = 0;
                foreach (var payment in this.RecieptListByRef)
                {
                    foreach (var detail in payment.RecieptListDetails.Where(o => o.RefID == RecieptListRefID))
                    {
                        amtReciept = amtReciept + detail.Amount.Value;
                    }
                }

                var SO = this.DataWorkspace.LSWData.SalesOrders.Where(o => o.DocumentID == RecieptListRefID).SingleOrDefault();

                if (SO == null) return;

                if (amtReciept >= SO.AmountTotal) return;

                var itemnew = this.RecieptListByRef.AddNew();

                if (SO.CustsVend != null)
                    itemnew.ObjectID = SO.CustsVend.CustVendID;

                var detailnew = itemnew.RecieptListDetails.AddNew();
                detailnew.RefID = RecieptListRefID;
                detailnew.Amount = SO.AmountTotal - amtReciept;

                IsLoaded = false;
                return;
            }

            if (IsOpenAndNew != null && IsOpenAndNew.Value)
            {
                RecieptListListAddAndEditNew_Execute();
                IsOpenAndNew = false;
                return;
            }
        }
    }
}
